package edu.csula.cs575;




import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;





public class MyMap  extends MapActivity implements LocationListener {
	ListView list;
	String[] mString = {"Hospital", "Pharmacy","Shelter","Stores"};
	ImageButton hideButton;
	private MapController mapController;
	private LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
         setContentView(R.layout.mymap);
         
        list = (ListView)this.findViewById(R.id.resourcelist1);
 		list.setItemsCanFocus(false);
 		list.setChoiceMode(ListView.PERSISTENT_ANIMATION_CACHE);
 		populateList();
 		if(this.getIntent().getExtras() !=null ){
 			populateMap(this.getIntent().getExtras().getInt("param"));
		}
 		hideList();
    }
    private void populateMap(int param) {
		// param is hospital,pharmacy,........
    	// dummy things here
    	
    	
    	MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		mapController = mapView.getController();
		mapController.setZoom(15);

		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
		GeoPoint geoPoint = new GeoPoint(34066614, -118166284);
		//		(int)(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude() * 1000000),
		//		(int)(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude() * 1000000));
		mapController.animateTo(geoPoint);

		MapOverlay posContainer = new MapOverlay(getResources().getDrawable(R.drawable.map_pin));
		OverlayItem posOverlay = new OverlayItem(geoPoint, "My Location", "You are here");
		posContainer.addOverlay(posOverlay);
		
		MapOverlay overlayList = new MapOverlay(getResources().getDrawable(overlayIcon[param]));
		
		for (OverlayItem i : testLoc[param])
			overlayList.addOverlay(i);

		List<Overlay> mapOverlay = mapView.getOverlays();

		if (posContainer.size() > 0)
			mapOverlay.add(posContainer);

		if (overlayList.size() > 0)
			mapOverlay.add(overlayList);
	}
	private void populateList() {
    	final ArrayList<ResourceView> image_details = GetSearchResults();
    	 
    	 list.setAdapter(new ResourceViewAdaptor(this, image_details));
    	 list.setOnItemClickListener(new OnItemClickListener() {
    	 public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
    		 MapView mapView = (MapView) findViewById(R.id.mapview);
    		 mapView.getOverlays().clear();
    		 mapView.invalidate();
    		 populateMap(position);
    		 
    		 
				
			}
    	 });
	}
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    private ArrayList<ResourceView> GetSearchResults(){
		ArrayList<ResourceView> results = new ArrayList<ResourceView>();

		ResourceView item_details = new ResourceView();
		item_details.setName("Hospital");
		item_details.setImageNumber(1);
		results.add(item_details);

		item_details = new ResourceView();
		item_details.setName("Pharmacy");
		item_details.setImageNumber(2);
		results.add(item_details);

		item_details = new ResourceView();
		item_details.setName("Stores");
		item_details.setImageNumber(3);
		results.add(item_details);

		item_details = new ResourceView();
		item_details.setName("Gas Station");
		item_details.setImageNumber(4);
		results.add(item_details);

		

		


		return results;
		}
    private void hideList() {
    	hideButton = (ImageButton) findViewById(R.id.hid_button);
        hideButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.arrow_down_float));
		
		hideButton.setOnClickListener(new View.OnClickListener() {
    		public void onClick(View v) {
    			LinearLayout lay1 = (LinearLayout)findViewById(R.id.linearLayout2);
				lay1.setVisibility(View.GONE);
				showList();
    		}
		});
		
	}
    private void showList() {
    	hideButton = (ImageButton) findViewById(R.id.hid_button);
        hideButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.arrow_up_float));
        hideButton.setOnClickListener(new View.OnClickListener() {
    		public void onClick(View v) {
    			LinearLayout lay1 = (LinearLayout)findViewById(R.id.linearLayout2);
				lay1.setVisibility(View.VISIBLE);
				hideList();
    		}
		});
		
	}
    public void onLocationChanged(Location location) {
		GeoPoint geoPoint = new GeoPoint(
				(int)(location.getLatitude() * 1000000),
				(int)(location.getLongitude() * 1000000));
		mapController.animateTo(geoPoint);		
	}
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	private static int[] overlayIcon =
		{
			R.drawable.hospital_icon3030,
			R.drawable.pharmacy3030,
			R.drawable.store3030,
			R.drawable.gas_station3030
		};

		private static OverlayItem[][] testLoc =
		{
		  	{
				new OverlayItem(new GeoPoint(34051415, -118163195), "Trinity Care", "")
			},

			{
				new OverlayItem(new GeoPoint(34076132, -118159128), "Verdugo Plaza Pharmacy", "")
			},

			{
				new OverlayItem(new GeoPoint(34073475, -118163624), "Valley Foods Liquor", "")
			},

			{
				new OverlayItem(new GeoPoint(34058979, -118172926), "Cheveron", "")
			}
		};
		private static String[] title =
			{
				"Alerts Screen",
				"Hazards Screen",
				"Resources Screen",
				"Services Screen"
			};
}